package com.icss.service.controller;

import com.icss.feign.model.User;
import com.icss.service.service.UserService;
import com.icss.service.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/* 用户服务*/
@RestController()
@RequestMapping(value  = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public Boolean register(@RequestBody User user){
        int insert = userService.register(user);
        if(insert > 0){
            return true;
        }
        return false;
    }

    @RequestMapping(value = "/account",method = RequestMethod.POST)
    public User queryByAccount(@RequestParam("account") String account){
        User user = userService.queryByAccount(account);
        return user;
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public void updateUserState(@RequestParam("userId")int userId){
        userService.updateUserState(userId);
    }

    @RequestMapping(value = "/upsert/session",method = RequestMethod.POST)
    public boolean saveSession(@RequestBody User user){
        if(user == null || user.getUser_id() <= 0){
            return false;
        }
        return SessionUtil.upsertSession(user);
    }

    @RequestMapping(value = "/query/session",method = RequestMethod.POST)
    public User querySession(@RequestParam("userId") int userId){
        return SessionUtil.querySession(userId);
    }
}
